<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/29 0029
 * Time: 9:59
 */
namespace App\FreeBird\Controllers\Admin;
use App\Base\Controllers\Controller;
use App\FreeBird\Contracts\InfoInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class InfoController extends Controller
{
    /**
     * 获取订单接口
     * @return InfoInterface
     */
    public function getInfoInterface(){
        return app()->make(InfoInterface::class);
    }

    /**
     * 订单管理
     * @param Request $request
     */
   public function index(Request $request){

       if($request->getMethod()=='POST'){

           $where=function($query) use ($request){
               $username=$request->input("username");
               $query->where("truename", "like", '%' . $username . '%');
           };

           return  $this->getInfoInterface()->getListAdmin($where);
       }
       return view("freebird.info.index");
   }



    /**
     * 经纬度
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request){
       return $this->getInfoInterface()->saveData($request->all());
    }

    /**
     * 审核通过
     * @param Request $request
     */
    public function applyTrue(Request $request){
        if($request->getMethod()=='POST') {
            $validator = Validator::make($request->all(), [
                'id' => 'required',
            ], ["id.required" => 'ID必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $id = $request->input("id");
            $where = function ($query) use ($id) {
                $query->where("id", $id);
            };
            $data["apply_status"] = '1';
            //审请通过后，就更新用户权限级
            return $this->getInfoInterface()->updateUserInfoTrueApply($where, $data);
        }
    }

    /**
     * 审核拒绝
     * @param Request $request
     * @return mixed
     * @throws \App\Base\Exceptions\ApiException
     */
    public function applyError(Request $request){
        if($request->getMethod()=='POST') {
            $validator = Validator::make($request->all(), [
                'id' => 'required',
                'msg' => 'required',
            ], ["id.required" => 'ID必须有值', "msg.required" => '拒绝理由必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $id = $request->input("id");
            $where = function ($query) use ($id) {
                $query->where("id", $id);
            };
            $msg=$request->input("msg");
            $data["apply_status"] = '-1';
            $data["apply_err_msg"] = $msg;
            return $this->getInfoInterface()->updateUserInfoApply($where, $data);
        }
    }

    public function show(Request $request){
        $id=$request->input("id");
        $info=$this->getInfoInterface()->getInfoMemberById($id);
        return view("freebird.info.show")->with("data",$info);
    }


}